<template>
  <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <div class="app-container">
      <el-button @click="handleAddGoodsGx" icon="el-icon-plus" plain size="mini" type="primary" >新增产品工序
      </el-button>
      <el-popconfirm :title="`确认删除数据项？`" @confirm="handleDelete({})"
                     style="margin-left: 10px;">
        <el-button slot="reference" type="danger" plain icon="el-icon-delete" :disabled="multiple" size="mini"
                   style="margin-left:10px;" >批量删除产品工序
        </el-button>
        <el-button slot="reference" type="success" plain icon="el-icon-delete" :disabled="multiple" size="mini"  @click="handleUpdateAll()"
                   style="margin-left:10px;" >批量修改
        </el-button>
      </el-popconfirm>
      <el-table v-loading="loading" :data="dataList" ref="table" :max-height="tableMaxHeight"
                style="width: 100%"   @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="序号" align="center" prop="xuhao"/>
        <el-table-column label="工序编码" align="center" prop="code"/>
        <el-table-column label="工序名称" align="center" prop="name"/>
        <el-table-column sortable align="center" label="是否最后一道工序" prop="islast" >
          <template slot-scope="scope" >
            <dict-tag :options="dict.type.yes_no" :value="scope.row.islast"/>
          </template>
        </el-table-column>
        <el-table-column label="车间" align="center" prop="deptName"/>
        <el-table-column label="标准工时" align="center" prop="workHour"/>
        <el-table-column label="工序产品名称" align="center" prop="gongxuName"/>
        <el-table-column label="工序产品型号" align="center" prop="gongxuModel"/>
        <el-table-column label="图纸号" align="center" prop="shuoming"/>
        <el-table-column label="工艺编码" align="center" prop="gongyiCode"/>
        <el-table-column label="工艺名称" align="center" prop="gongyiName"/>
        <el-table-column label="总数量" align="center" prop="counts"/>
        <el-table-column label="已分配数量" align="center" prop="fenpeiCount"/>
        <!--<el-table-column label="剩余分配数量" align="center" prop="remainCount"/>-->
        <el-table-column label="备注" align="center" prop="remark"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-popconfirm :title="`确认设为最后一道工序当前数据项？`" @confirm="setLast(scope.row)"
                           style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                         v-hasPermi="['develop:LnGongyigongxu:remove']">设为最后一道工序</el-button>
            </el-popconfirm>
            <el-popconfirm :title="`确认删除数据项？`" @confirm="handleDelete(scope.row)"
                           style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete" >删除</el-button>
            </el-popconfirm>
          </template>
      </el-table-column>
      </el-table>

      <goods-gongxu-add-form ref="goodsGongxuForm" @ok="getList()"></goods-gongxu-add-form>
      <!--<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>-->
      <goodsSaleDoGongxuForm-all ref="formAll" @ok="getList()"></goodsSaleDoGongxuForm-all>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { deleteAction, getAction } from '@/api/manage'
  import GoodsGongxuAddForm from '@/views/develop/goodsSaledoGongxu/module/GoodsGongxuAddForm'
  import GoodsSaleDoGongxuFormAll from '@/views/develop/lnGongxu/module/GoodsSaleDoGongxuFormAll'

  export default {
    name: "GoodsSaleGongxuForm",
    dicts: ['yes_no'],
    components: {
      GoodsGongxuAddForm,
      GoodsSaleDoGongxuFormAll,
    },
    data() {
      return {
        activeName: "first",
        // 弹出层标题
        title: "产品工序查看2",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        saleId:null,
        goodsCount:null,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单产品工艺工序关联表格数据
        dataList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10000,
          xuhao: null,
          code: null,
          name: null,
          shuoming: null,
          file: null,
          photo: null,
          status: null,
          saleId: null,
          goodsId: null,
          gongyiId: null,
          gongxuId: null,
          gongyiCode: null,
          gongyiName: null,
          deptId: null,
          deptName: null,
          workHour: null,
        /*  orderByColumn: "xuhao",saleId
          isAsc: "asc",*/
        },
        url: {
          list:'/goodsSaledoGongxu/goodsSaledoGongxu/list',
          remove:'/goodsSaledoGongxu/goodsSaledoGongxu/remove/',
          setLast:'/goodsSaledoGongxu/goodsSaledoGongxu/setLast',
        },
        tableMaxHeight: window.innerHeight - 320,
      };
    },

    methods: {
      setLast(row) {
        let params ={"id":row.id,"saleId" : this.queryParams.saleId}
        getAction(this.url.setLast, params).then(response => {
          this.$modal.msgSuccess("设定成功");
          this.getList();
        }).catch(() => {});
      },
      handleUpdateAll(){
        const ids =  this.ids;
        this.$refs.formAll.handleUpdate(ids);
      },
      handleAddGoodsGx(){
        this.$refs.goodsGongxuForm.handleAddGoodsGongxu(this.saleId,this.goodsCount);
      },
      handleDelete(row) {
        const ids = row.id || this.ids;
        deleteAction(this.url.remove, ids).then(response => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      getList(){
        getAction(this.url.list, this.queryParams).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 查询订单产品工艺工序关联列表 */
      xunjianShow(row,show) {
        if(show) {
          this.loading = true;
          this.open=true;
          this.queryParams.saleId=row.goodsSaleId;
          this.getList();
        } else {
          this.loading = true;
          this.open=true;
          this.queryParams.saleId=row.id;
          this.saleId=row.id;
          this.goodsCount=row.count;
          this.getList();
        }

      },
      getTableHeight () {
        this.tableHeight = window.innerHeight - 320
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
    }
  };
</script>
